package com.it.glide.glidesample;

import android.content.Context;
import android.os.Environment;

import com.bumptech.glide.Glide;
import com.bumptech.glide.GlideBuilder;
import com.bumptech.glide.load.DecodeFormat;
import com.bumptech.glide.load.engine.cache.DiskLruCacheFactory;
import com.bumptech.glide.load.engine.executor.FifoPriorityThreadPoolExecutor;
import com.bumptech.glide.module.GlideModule;

import java.io.InputStream;

public class SimpleGlideModule implements GlideModule {

   /* 1、可以全局的改变 glide 的加载策略

    2、可以自定义磁盘缓存目录

    3、可以设置图片加载的质量*/


 
    @Override
    public void applyOptions(Context context, GlideBuilder builder) {
        //定义缓存大小为100M
        int  diskCacheSize =  100 * 1024 * 1024;
 
        //自定义缓存 路径 和 缓存大小
        String diskCachePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/glideCacheSimpleGlideModule" ;
 
        //提高图片质量
        builder.setDecodeFormat(DecodeFormat.PREFER_RGB_565);
 
        //自定义磁盘缓存:这种缓存只有自己的app才能访问到
        // builder.setDiskCache( new InternalCacheDiskCacheFactory( context , diskCacheSize )) ;
        // builder.setDiskCache( new InternalCacheDiskCacheFactory( context , diskCachePath , diskCacheSize  )) ;
 
        //自定义磁盘缓存：这种缓存存在SD卡上，所有的应用都可以访问到
        builder.setDiskCache(new DiskLruCacheFactory(diskCachePath , diskCacheSize));
//        builder.setDiskCacheService()
        builder.setDiskCacheService(new FifoPriorityThreadPoolExecutor(10));
        builder.setResizeService(new FifoPriorityThreadPoolExecutor(8));
//        builder.set
    }
 
    @Override
    public void registerComponents(Context context, Glide glide) {
        glide.register(WebServiceGlideUrl.class, InputStream.class, new WebServiceGlideUrlLoader.Factory());
    }
}